#include "list.h"

P_Node_t Init_Get_Node(DataTypef *newdata)
{
    P_Node_t Newnode = calloc( 1 , sizeof(Node_t));

    if (newdata)
    {
        memcpy( &Newnode->Data , newdata , sizeof(DataTypef));
    }
    
    Newnode->Next = Newnode->Prev = Newnode;


    return Newnode;
}

bool Cmp_Num(DataTypef *A , DataTypef *B)
{
    return A->Name[0] > B->Name[0];
}

bool Cmp_Name(DataTypef *A , DataTypef *B)
{
    return A->TV[0] > B->TV[0];
}

bool Cmp_Score(DataTypef *A , DataTypef *B)
{
    return A->Score > B->Score;
}

void Sorting(P_Node_t head , P_Node_t newdata , Cmp_Ok cmp)
{
    P_Node_t tmp = NULL;
    for (tmp = head->Next; tmp != head; tmp = tmp->Next)
    {
        if (cmp(&tmp->Data , &newdata->Data))
        {
            break;
        }
    }
    Head2add( tmp->Prev , newdata , tmp);
}

void Head2add(P_Node_t prev , P_Node_t newnode , P_Node_t next)
{
    prev->Next = newnode;
    newnode->Next = next;

    next->Prev = newnode;
    newnode->Prev = prev;

    return ;
}

void DisPlay(P_Node_t head , Print pri)
{
    for (P_Node_t tmp = head->Next; tmp != head; tmp = tmp->Next)
    {
        pri(&tmp->Data);
    }  
}

// bool Com_Size(DataTypef* A , DataTypef *B)
// {
//     if (strcmp(A->Name , B->Name) == 0)
//     {
//         return true;
//     }
// }

P_Node_t Find_fun(P_Node_t head , char * find)
{
    P_Node_t tmp = NULL;
    for (tmp = head->Next; tmp != head; tmp = tmp->Next)
    {
       if (memcmp(tmp->Data.Name , find , sizeof(32)))
       {
        printf("找到目标\n");
        break;
       }
    }
    return tmp;
}

void Del_Fun( P_Node_t Prev , P_Node_t Del , P_Node_t Next)
{
    Prev->Next = Del->Next;
    Next->Prev = Del->Prev;

    Del->Next = Del->Prev = Del;
}

P_Node_t Update(P_Node_t Name)
{

    DataTypef newdata = Get_New_Data();

    memcpy(&Name->Data ,&newdata , sizeof(DataTypef));

    return Name;
}

void Destroy(P_Node_t head , P_Node_t node)
{
    if (head->Next == node)
    {
        free(head);
        return ;
    }
    
    Destroy(head->Next,node);

    free(head);
    return ;
}

